package com.example.backend.controller;


import com.example.backend.pojo.Result;
import com.example.backend.pojo.User;
import com.example.backend.service.UserService;
import com.example.backend.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/register")//注册
    public Result register( String username, String password){
        User user=userService.getUserByName(username);
        if(user==null){
            userService.register(username, password);
            return Result.success();
        }else{
            return Result.error("用户注册失败");
        }
    }

    @PostMapping("/login")//登录
    public Result<String> login( String username, String password){
        User user=userService.getUserByName(username);
        //查看用户状态是否注册，再检查用户名是否纯在，并且检查是否密码正确
        if(user==null){
            return Result.error("用户名错误");
        }
        if(Md5Util.getMD5String(password).equals(user.getPassword())){
            Map<String ,Object> claims=new HashMap<>();
            // 登录成功后会获得用户id和用户名
            claims.put("id",user.getUserid());
            claims.put("username",user.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/userInfo")//获得用户的基本信息
    public Result<User> userInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userService.getUserByName(username);
        return Result.success(user);
    }

    @DeleteMapping("/delete")
    public Result delete(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userService.delete(id);
        return Result.success("成功");
    }

    @PutMapping("/relive")
    public Result relive(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userService.relive(id);
        return Result.success("成功");
    }


    @PutMapping("/update")//用户基本信息进行更新
    public Result update(@RequestBody User user){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userid = (Integer) map.get("id");
        user.setUserid(userid);
        userService.update(user);
        return Result.success("成功");
    }

    @PostMapping("/updateAvatar")//图片更新
    public  Result updateAvatar(MultipartFile file) throws IOException {
//        String originalFilename = file.getOriginalFilename();
//        String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
//        String url = AliOssUtil.uploadFile(filename, file.getInputStream());

        String url = TencentCOSUtil.upLoadFile(file);
        userService.updateAvatar(url);
        return  Result.success();
    }

    @PatchMapping("/updatePwd")//密码更新
    public  Result updatePwd(@RequestBody Map<String,Object> params){
        String old_pwd = (String) params.get("old_pwd");
        String new_pwd = (String) params.get("new_pwd");
        String re_pwd = (String) params.get("re_pwd");
        if(!StringUtils.hasLength(old_pwd) ||!StringUtils.hasLength(new_pwd)||!StringUtils.hasLength(re_pwd)){
            return Result.error("缺少参数");
        }
        Map<String,Map> loginUser = ThreadLocalUtil.get();
        User user = userService.getUserByName(String.valueOf(loginUser.get("username")));

        if(!user.getPassword().equals(Md5Util.getMD5String(old_pwd))){
            return Result.error("密码错误");
        }

        if(!new_pwd.equals(re_pwd)){
            return Result.error("新密码不相等");
        }
        new_pwd=Md5Util.getMD5String(new_pwd);
        userService.updatePwd(user.getUserid(),new_pwd);
        return Result.success("成功");
    }

    @PostMapping("/updatevip")
    public Result updatevip(Integer month){

        userService.updatevip(month);
        return  Result.success();
    }

    @PostMapping("getmoney")
    public Result<Double> getmoney(Integer userid){
        return Result.success(userService.getmoney(userid));
    }
    @PostMapping("updatemoney")
    public Result updatemoney(Double money,Integer userid)
    {
        userService.updatemoney(money,userid);
        return Result.success();
    }


}
